Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

Add Duration.isoformat()#522

Open
seandstewart wants to merge 5 commits intopython-pendulum:master from
seandstewart:add-duration-isoformat
Open

Add Duration.isoformat() #522
seandstewart wants to merge 5 commits intopython-pendulum:master from
seandstewart:add-duration-isoformat

Conversation

@seandstewart
Copy link

@seandstewart seandstewart commented Nov 23, 2020

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

This adds a simple method to represent Durations in ISO format. Given that pendulum can parse ISO 8601 strings into Durations, it makes sense that we'd be able to also serialize the other way.

Copy link
Author

@sdispater looks like the only failures left are on Python2.7 and unrelated to my changes.

Any thoughts on this feature?

```

Finally, it has an
[ISO-8601-compliant](https://en.wikipedia.org/wiki/ISO_8601#Durations) `isformat()`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant isoformat()

Copy link

IceN9ne commented Jan 17, 2021
edited
Loading

I would really like this feature, but it seems that even the competing PR #314 released almost 2 years prior to this one has no dev comments as well. Hoping one of these two makes it in eventually.

I did notice this PR doesn't drop the unnecessary portions of the string for 0 values, which makes the resulting format quite verbose.

Copy link

Is there some hold up on this? Lack of interest? Given how Pendulum has a formatter for datetime objects it seems strange there's not at least some nod towards providing one for durations.

Copy link
Collaborator

Secrus commented Jan 3, 2025

Is there some hold up on this? Lack of interest? Given how Pendulum has a formatter for datetime objects it seems strange there's not at least some nod towards providing one for durations.

The commits in this PR are 4+ years old at this point and significantly out of date with the current state of the codebase. If you are interested, feel free to make a new PR for this feature, superseding this one.

Comment on lines +419 to +423
periods = [
("Y", self.years),
("M", self.months),
("D", self.remaining_days),
]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing weeks,

Suggested change
periods = [
("Y", self.years),
("M", self.months),
("D", self.remaining_days),
]
periods = [
("Y", self.years),
("M", self.months),
("W", self.weeks),
("D", self.remaining_days),
]

]
period = "P"
for sym, val in periods:
period += "{val}{sym}".format(val=val, sym=sym)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you only keep the components that are set, .isoformat() becomes a proper inverse transformation of str → Duration

assert pendulum.parse("P1W").isoformat() == "P1W"

need same change below obviously

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers

@jgogstad jgogstad jgogstad left review comments

@IceN9ne IceN9ne IceN9ne left review comments

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /